package com.qingmi.farmer.ui.buy;

import android.content.Context;
import android.content.Intent;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.view.View;

import com.gyf.barlibrary.ImmersionBar;
import com.qingmi.farmer.R;
import com.qingmi.farmer.data.ActionsCode;
import com.qingmi.farmer.data.DataSetting;
import com.qingmi.farmer.data.UserData;
import com.qingmi.farmer.data.model.GoodsInfo;
import com.qingmi.farmer.data.repository.data.GoodsDataRepository;
import com.qingmi.farmer.databinding.ActivityGoodsDetailBinding;
import com.wjjun.base.ui.base.BaseActivity;
import com.qingmi.farmer.window.EditCountDialog;
import com.wjjun.basemvpvm.BR;
import com.wjjun.basemvpvm.data.exception.MyException;
import com.wjjun.basemvpvm.data.observer.DefaultObserver;
import com.wjjun.basemvpvm.data.request.SimpleRequest;
import com.wjjun.customview.window.BaseDialog;
import com.wjjun.viewmodel.BannerItemViewModel;
import com.youth.banner.listener.OnBannerListener;

import java.util.List;

import javax.inject.Inject;

import me.tatarka.bindingcollectionadapter2.ItemBinding;


/**
 * 商品详情
 */
public class GoodsDetailActivity extends BaseActivity<GoodsDetailViewModel, ActivityGoodsDetailBinding> {


    public static void start(Context context, String id) {
        Intent intent = new Intent(context, GoodsDetailActivity.class);
        intent.putExtra(ActionsCode.DATA_ID, id);
        context.startActivity(intent);
    }

    public static void start(Context context, String id, int count, String shopCardId) {
        Intent intent = new Intent(context, GoodsDetailActivity.class);
        intent.putExtra(ActionsCode.DATA_ID, id);
        intent.putExtra(ActionsCode.DATA_COUNT, count);
        intent.putExtra(ActionsCode.DATA_ID2, shopCardId);
        context.startActivity(intent);
    }

    private ObservableList<BannerItemViewModel> bannerItems = new ObservableArrayList<>();

    private EditCountDialog editCountDialog;

    @Inject
    GoodsDataRepository goodsDataRepository;

    @Inject
    UserData userData;

    private String id;
    private String shopCardId;

    private int defCount;

    @Override
    protected void initImmersionBar() {
        ImmersionBar.with(this).titleBarMarginTop(R.id.app_bar)
                .keyboardEnable(true).statusBarDarkFont(true, 0.7f).init(); //初始化，默认透明状态栏和黑色导航栏
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_goods_detail;
    }

    @Override
    protected GoodsDetailViewModel initViewModel() {
        return new GoodsDetailViewModel();
    }

    @Override
    protected void initContent() {
        goodsDataRepository.bind(this);
        super.initContent();

        viewModel.itemBinding.set(ItemBinding.of(BR.viewModel, R.layout.layout_item_param));

        id = getIntent().getStringExtra(ActionsCode.DATA_ID);
        shopCardId = getIntent().getStringExtra(ActionsCode.DATA_ID2);
        defCount = getIntent().getIntExtra(ActionsCode.DATA_COUNT, 1);
    }

    @Override
    protected void initData() {
        super.initData();
        goodsDataRepository.getGoodsDetail(new DefaultObserver<GoodsInfo>() {
            @Override
            public void onNext(GoodsInfo goodsInfo) {
                if (goodsInfo == null) {
                    showToast(R.string.error_goods_is_empty);
                    finish();
                    return;
                }
                viewModel.bindData(goodsInfo);
                setBannerData(goodsInfo.getPic(), goodsInfo.getPics());
                setCount(defCount);
            }

            @Override
            public void onError(MyException e) {
                showToast(e.getMessage());
                finish();
            }
        }, new SimpleRequest(id));
    }


    @SuppressWarnings("ConstantConditions")
    private void setBannerData(String def, List<String> imgList) {
        bannerItems.clear();
        if (imgList == null || imgList.isEmpty()) {
            bannerItems.add(new BannerItemViewModel(DataSetting.getPicUrl(def)));
        } else {
            for (String item : imgList) {
                bannerItems.add(new BannerItemViewModel(DataSetting.getPicUrl(item)));
            }
        }

        viewModel.bannerViewModel.get().setItems(bannerItems);
    }


    @SuppressWarnings("ConstantConditions")
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_back:  //返回
                finish();
                break;
            case R.id.iv_service:   //客服
                break;
            case R.id.iv_trolley:   //购物车
                break;
            case R.id.tv_count: //修改数量
                showEditCountDialog(0);
                break;
            case R.id.tv_add_trolley:   //加入购物车
                break;
            case R.id.tv_reserve:   //立即预定
                break;
        }
    }

    /**
     * @param auto 0自动隐藏, 1:自动加入购物车, 2:自动预定
     */
    @SuppressWarnings("ConstantConditions")
    private void showEditCountDialog(final int auto) {
        if (editCountDialog == null) {
            editCountDialog = new EditCountDialog(this);
            editCountDialog.setOnDialogListener(new BaseDialog.OnDialogListener() {
                @Override
                public void onCancel() {
                    editCountDialog.dismiss();
                }

                @Override
                public void onConfirm(Object value) {
                    setCount((Integer) value);
                    editCountDialog.dismiss();
                }
            });
        }
        editCountDialog.setCount(viewModel.count.get());
        editCountDialog.setMaxCount(viewModel.inventory.get());
        editCountDialog.show();
    }

    @SuppressWarnings("ConstantConditions")
    private void setCount(int count) {
        viewModel.count.set(count);
        viewModel.totalPrice.set(count * viewModel.price.get());
    }


    //banner的点击事件
    public OnBannerListener bannerListener = new OnBannerListener() {
        @Override
        public void OnBannerClick(int position) {
        }
    };
}
